/**
 * BBD Service Inc
 * All Rights Reserved @2018
 */
package com.magicmovie.web.controller;

import com.magicmovie.entity.Dictionary;
import com.magicmovie.entity.IndexCategory;
import com.magicmovie.entity.IndexMovie;
import com.magicmovie.entity.Movie;
import com.magicmovie.utils.AjaxMsg;
import com.magicmovie.utils.ValidUtil;
import com.magicmovie.web.service.*;
import org.apache.log4j.Category;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页设置管理
 *
 * @author
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private IndexCategoryService indexCategoryService;
    @Resource
    private IndexMovieService indexMovieService;
    @Resource
    private MovieService movieService;
    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping(value = "/list")
    public String list(Model model) {
        Map map = new HashMap<>();
        List list = indexCategoryService.selectByParams(map);
        model.addAttribute("list", list);

        List<Map<String, Object>> movies = movieService.queryList(map);
        model.addAttribute("movies", movies);

        List<Dictionary> dictionaries = dictionaryService.queryByParams(map);
        model.addAttribute("dictionaries", dictionaries);
        return "category/list";
    }

    @RequestMapping(value = "/movies")
    @ResponseBody
    public AjaxMsg movies(Integer indexCategoryId) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Map map = new HashMap<>();
        map.put("indexCategoryId", indexCategoryId);
        List list = indexMovieService.selectByParams(map);
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        ajaxMsg.setDatas(list);
        return ajaxMsg;
    }

    @RequestMapping(value = "/input")
    public String input(Model model, Integer id) {
        String isEdit;
        if (id != null && id != 0) {
            isEdit="edit";
            IndexCategory category = indexCategoryService.selectByPrimaryKey(id);
            model.addAttribute("entity", category);
        } else {
            isEdit="add";
            model.addAttribute("entity", new IndexCategory());
        }
        model.addAttribute("isEdit",isEdit);
        return "category/input";
    }

    @RequestMapping(value = "/save")
    public String save(IndexCategory indexCategory) {
        if (indexCategory.getId() == null || indexCategory.getId() == 0) {
            indexCategoryService.insert(indexCategory);
        } else {
            indexCategoryService.updateByPrimaryKey(indexCategory);
        }
        return "redirect:/category/list";
    }

    @RequestMapping(value = "/addMovie")
    @ResponseBody
    public AjaxMsg addMovie(Integer indexCategoryId, Integer[] ids, HttpServletRequest request) {
        for (Integer movieId : ids) {
            IndexMovie movie = new IndexMovie();
            movie.setIndexCategoryId(indexCategoryId);
            movie.setMovieId(movieId);
            indexMovieService.insert(movie);
        }
        return new AjaxMsg();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public AjaxMsg delete(Integer indexCategoryId) {
        Map map = new HashMap();
        AjaxMsg ajaxMsg =new AjaxMsg();
        map.put("indexCategoryId", indexCategoryId);
        List list = indexMovieService.selectByParams(map);
        if (!ValidUtil.isEmpty(list)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("该分类中存在电影，不可删除");
            return ajaxMsg;
        }else{
            indexCategoryService.deleteByPrimaryKey(indexCategoryId);
        }
        return new AjaxMsg();
    }

    @RequestMapping(value = "/removeMovie", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMsg addMovie(Integer id) {
        indexMovieService.deleteByPrimaryKey(id);
        return new AjaxMsg();
    }
}
